/**
 * Created by major on 2017/7/4.
 */
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

const store = new Vuex.Store({
  // 定义状态
  state: {
    user: {
      userName: '',
      role: ''
    },
    nav: [
      {
        navName: '首页',
        routeUrel: '/app'
      }
    ]
  },
  mutations: {
    setNav(state, navs) {
      state.nav = navs
    },
    login(state, user){
      state.user.userName = user.userName
      state.user.role = user.role
    },
    testLogin(){
      let isLogin = false
      this.http.get("user/islogin")
        .then((response) => {
          let responseData = response.data
          console.log('login request success', responseData)
          if (responseData.flag === this.EXECUTE_OK) {
            console.log(responseData.data)
            isLogin = responseData.data
          } else {
            self.$message.error('登录失败，' + responseData.message);
          }
        })
        .catch(function (response) {
          self.$message.error('登录异常，请联系程序员小哥排查');
        })
      return isLogin
    }
  },
  getters: {
    getLoginUser: state => {
      return state.user
    },
    isLogin: state => {
      console.log('userName:',state.userName)
      let isLogin = state.userName === ''
      console.log('isLogin', isLogin)
      if (isLogin === false) {
        axios.get("user/islogin")
          .then((response) => {
            let responseData = response.data
            console.log('login request success', responseData)
            if (responseData.flag === 0) {
              console.log(responseData.data)
              isLogin = responseData.data
            } else {
              self.$message.error('登录失败，' + responseData.message);
            }
          })
          .catch(function (response) {
            console.log(response)
          })
      }
      return isLogin
    }
  }
})

export default store
